<?php
namespace backend\controllers;

use backend\models\Admin;
use backend\models\GroupCat;
use common\components\BaseController;
use SebastianBergmann\CodeCoverage\Report\Xml\Project;
use Yii;
use yii\db\Query;
use \common\helps\tools;
use \common\helps\code;

/**
 * User controller
 */
class GroupController extends BaseController
{

    public $modelClass = 'backend\models\GroupCat';

    /*
     * 群组分类列表
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionGroupCatList()
    {
        $model = new \backend\models\GroupCat();
        $request =$this->request;
        $params = $request->queryParams;
        $total = $model::selectData($params);
        $res = tools::getPageData($total,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);
        if(!empty($params['excel_download']) && $params['excel_download']>0){
            $tableHeader = ['id','标题','简介','状态'];
            $data = [];
            foreach ($total as $k => $v){
                $data[$k]['id'] = $v['id'];
                $data[$k]['name'] = $v['name'];
                $data[$k]['describe'] = $v['describe'];
                if($v['status'] == 1){
                    $status = '正常';
                }else{
                    $status = '禁用';
                }
                $data[$k]['status'] = $status;
            }
            tools::ToPHPEXECL($tableHeader,$data,'群组分类列表');
        }
        return code::success($res);
    }

    /*
     * 添加群组分类
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionAddGroupCat()
    {
        $model = new \backend\models\GroupCat();
        $request =$this->request;
        $params = $request->bodyParams;
        $res = $model::addData($params);
        if($res){
            return code::success($res);
        }else{
            return code::error('error');
        }
    }

    /*
     * 任务详情
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionGroupCatDetail(){
        $model = new \backend\models\GroupCat();
        $request =$this->request;
        $params = $request->bodyParams;
        $data = $model::findOneData($params['id']);
        return code::success($data);
    }

    /*
     * 修改项目分类
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionSaveGroupCat()
    {
        $model = new \backend\models\GroupCat();
        $request =$this->request;
        $params = $request->bodyParams;
        $id = $params['id'];
        unset($params['id']);
        $res = $model::saveData($id,$params);
        return code::success($res);
    }

    /*
     * 删除项目分类
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionDeleteGroupCat()
    {
        $model = new \backend\models\GroupCat();
        $request =$this->request;
        $params = $request->bodyParams;
        $res = $model::deleteData($params['id']);
        return code::success($res);
    }

    /*
     * 群组列表
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionGroupList()
    {
        $model = new \backend\models\Group();
        $request =$this->request;
        $params = $request->queryParams;
        //$params = $request->bodyParams;
        $total = $model::selectData($params,'`group`.`*`,`i`.`name` as `group_cat_name`');
        $res = tools::getPageData($total,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);
        if(!empty($params['excel_download']) && $params['excel_download']>0){
            $tableHeader = ['id','名称','简介','群组分类','入群经验值','管理员id','管理员','创建时间'];
            $data = [];
            foreach ($total as $k => $v){
                $data[$k]['id'] = $v['id'];
                $data[$k]['name'] = $v['name'];
                $data[$k]['describe'] = $v['describe'];
                $data[$k]['group_cat_name'] = $v['group_cat_name'];
                $data[$k]['experience'] = $v['experience'];
                $data[$k]['admin_id'] = $v['admin_id'];
                $data[$k]['admin_name'] = $v['admin_name'];
                $data[$k]['created_at'] = date('Y-m-d H:i:s',$v['created_at']);
            }
            tools::ToPHPEXECL($tableHeader,$data,'群组列表');
        }
        return code::success($res);
    }

    /*
     * 添加群组
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionAddGroup()
    {
        $model = new \backend\models\Group();
        $admin = new Admin();
        $groupCat = new GroupCat();
        $project = new \backend\models\Project();
        $request =$this->request;
        $params = $request->bodyParams;
        if(!empty($params)) {
            if ($model->load($params, '')) {
                if ($model->validate()) {
                    $res = $model->saveInfo('create',$params);
                    if(empty($res->errorInfo)){
                        return code::success('');
                    }else{
                        return code::error('error');
                    }
                }else{
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
            }
        }else{
            $data['adminInfo'] = $admin->selectData('`id`,`username`');
            $data['groupCatInfo'] = $groupCat->findAllData('',1,1,1000);
            $data['project'] = $project::findAllData();
            return code::success($data);
        }
    }

    /*
     * 群组详情
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionGroupDetail(){
        $model = new \backend\models\Group();
        $request =$this->request;
        $params = $request->bodyParams;
        $data = $model::findOneData($params['id']);
        return code::success($data);
    }

    /*
     * 修改群组
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionSaveGroup()
    {
        $model = new \backend\models\Group();
        $admin = new Admin();
        $groupCat = new GroupCat();
        $project = new \backend\models\Project();
        $request =$this->request;
        $params = $request->bodyParams;
        $id = $params['id'];
        unset($params['id']);
        if(!empty($params)) {
            if ($model->load($params, '')) {
                if ($model->validate()) {
                    $res = $model->saveInfo('update',$params,$id);
                    if(empty($res->errorInfo)){
                        return code::success('');
                    }else{
                        return code::error('error');
                    }
                }else{
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
            }
        }else{
            $data['group'] = $model::findOneData($id);
            $data['adminInfo'] = $admin->findAllData();
            $data['groupCatInfo'] = $groupCat->findAllData('','',1,1000);
            $data['project'] = $project::findAllData();
            return code::success($data);
        }
    }

    /*
     * 删除群组
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionDeleteGroup()
    {
        $model = new \backend\models\Group();
        $id = \Yii::$app->getRequest()->post('id');
        $res = $model::deleteData($id);
        return code::success($res);
    }


}
